package cn.nexd.map.rendering.overlay;

import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.TypedValue;
import android.view.MotionEvent;

import cn.nexd.map.rendering.core.SVGMapBaseOverlay;
import cn.nexd.map.rendering.core.componet.SVGMapView;


/**
 * Created by codingfish on 15/7/9.
 */
public class SVGMapLineOverlay extends SVGMapBaseOverlay {


    // Color for compass
    private static final int DEFAULT_LOCATION_COLOR = 0xFF3EBFC9;
    private static final int DEFAULT_LOCATION_SHADOW_COLOR = 0xFF909090;
    private float defaultLocationCircleRadius;


    private Paint locationPaint;

    private PointF startPosition = null;
    private PointF endPosition;
    private SVGMapView svgMapView;

    public SVGMapLineOverlay(SVGMapView svgMapView) {
        this.svgMapView = svgMapView;
        initLayer(svgMapView);
    }

    private void initLayer(SVGMapView svgMapView) {
        this.showLevel = LOCATION_LEVEL - 1;
        //
        locationPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        locationPaint.setAntiAlias(true);
        locationPaint.setStyle(Paint.Style.FILL);
        locationPaint.setColor(DEFAULT_LOCATION_COLOR);
        locationPaint.setStrokeWidth(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, svgMapView.getResources().getDisplayMetrics()));
        locationPaint.setShadowLayer(5, 3, 3, DEFAULT_LOCATION_SHADOW_COLOR);
        //
        defaultLocationCircleRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, svgMapView.getResources().getDisplayMetrics());
        //

    }


    /**
     * 设置需要打点的起始位置以及终止位置。
     *
     * @param startPosition
     * @param endPosition
     */
    public void setPosition(PointF startPosition, PointF endPosition) {
        this.startPosition = new PointF(startPosition.x - svgMapView.getFloorFeature().getOffset_x(), startPosition.y - svgMapView.getFloorFeature().getOffset_y());
        this.endPosition = new PointF(endPosition.x - svgMapView.getFloorFeature().getOffset_x(), endPosition.y - svgMapView.getFloorFeature().getOffset_y());
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onTap(MotionEvent event) {

    }

    @Override
    public void draw(Canvas canvas, Matrix matrix, float currentZoom, float currentRotateDegrees) {
        canvas.save();

        if (this.isVisible && this.startPosition != null && this.endPosition != null) {

            float startPositions[] = {startPosition.x, startPosition.y};
            float endPositions[] = {endPosition.x, endPosition.y};

            matrix.mapPoints(startPositions);

            matrix.mapPoints(endPositions);
            // 绘制一个圆形
            // 原点的XY坐标、半径以及Paint
            canvas.drawCircle(startPositions[0], startPositions[1], defaultLocationCircleRadius, locationPaint);

            canvas.drawCircle(endPositions[0], endPositions[1], defaultLocationCircleRadius, locationPaint);

            canvas.drawLine(startPositions[0], startPositions[1], endPositions[0], endPositions[1], locationPaint);
        }
        canvas.restore();
    }
}
